package com.play.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.play.common.utils.SnowFlakeUtil;
import com.play.dto.PrescriptionDTO;
import com.play.entity.Prescription;
import com.play.mapper.PrescriptionDetailMapper;
import com.play.mapper.PrescriptionMapper;
import com.play.service.IPrescriptionService;

@Service
public class PrescriptionServiceImpl implements IPrescriptionService {
	
	@Autowired
	private PrescriptionMapper prescriptionMapper;
	@Autowired
	private PrescriptionDetailMapper prescriptionDetailMapper;

	@Override
	public int addPrescription(Prescription prescription) {
		
		if (StringUtils.isBlank(prescription.getName())) {
			return 0;
		}
		
		Prescription p = selectPrescriptionByName(prescription.getName());
		if (null != p) {
			return 0;
		}
		prescription.setId(SnowFlakeUtil.getId());
		prescription.setCdate(new Date());
		prescription.setMdate(new Date());
		return prescriptionMapper.insert(prescription);
	}

	@Override
	public int updatePrescription(Prescription prescription) {
		prescription.setMdate(new Date());
		return prescriptionMapper.updateByPrimaryKeySelective(prescription);
	}

	@Override
	public Prescription selectPrescriptionByName(String name) {
		return prescriptionMapper.selectPrescriptionByName(name);
	}

	@Override
	public Prescription selectPrescriptionById(Long id) {
		return prescriptionMapper.selectByPrimaryKey(id);
	}

//	@Override
//	public PageInfo<Prescription> selectPrescriptionList(int pageNum, int pageSize, String name, String description) {
//		PageHelper.startPage(pageNum, pageSize);
//		PageInfo<Prescription> pageInfo = new PageInfo<>(prescriptionMapper.selectPrescriptionList(name, description));
//		return pageInfo;
//	}

	@Override
	public PageInfo<PrescriptionDTO> selectPrescriptionList(int pageNum, int pageSize, String name, String description) {
		PageHelper.startPage(pageNum, pageSize);
		List<PrescriptionDTO> list = prescriptionMapper.tst();
		PageInfo<PrescriptionDTO> pageInfo = new PageInfo<>(list);
		for (PrescriptionDTO prescription : list) {
			prescription.setPrescriptionDetailList(prescriptionDetailMapper.getDetailByPrescriptionId(prescription.getId()));
		}
		
		return pageInfo;
	}

}
